package com.szliang.threadlocal;

/**
 * 描述：ThreadLocal用法2：避免传递参数的麻烦
 */
public class ThreadLocalNormalUsage06 {

    public static void main(String[] args) {
        new Service1().process();
    }
}

class Service1 {

    public void process() {
        User user = new User("刘佳静");
        UserContextHolder.userHolder.set(user);
        new Service2().process();
    }
}

class UserContextHolder {

    public static ThreadLocal<User> userHolder = new ThreadLocal<>();
}

class Service2 {

    public void process() {
        User user = UserContextHolder.userHolder.get();
        System.out.println("Service2获取用户对象："+user.getName());
        new Service3().process();
    }
}

class Service3 {

    public void process() {
        User user = UserContextHolder.userHolder.get();
        System.out.println("Service3获取用户对象："+user.getName());
        UserContextHolder.userHolder.remove();
    }
}

class User {

    private String name;

    public User(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }
}
